package com.java.netty.common.helper;


import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.java.netty.common.device.AppInfo;
import com.java.netty.common.device.DeviceEnvInfo;
import com.java.netty.common.device.DeviceExtInfo;
import com.java.netty.common.device.DeviceInfo;

public class PptpJsonDataHelper {
    public static JSONObject getEnvAJsonData(DeviceInfo deviceInfo) {
        JSONObject jRoot = new JSONObject();
        JSONObject jEnv = new JSONObject();
        JSONObject jExt = new JSONObject();
        try {
            jRoot.put("udid", deviceInfo.getClientId());//udid is clientId
            jRoot.put("deviceName", deviceInfo.getDeviceName());

            DeviceEnvInfo deviceEnvInfo = deviceInfo.getDeviceEnvInfo();
            jEnv.put("code", deviceEnvInfo.getDeviceCode());
            jEnv.put("sn", deviceEnvInfo.getDeviceSn());
            jEnv.put("type", deviceEnvInfo.getDeviceType());
            jRoot.put("env", jEnv);

            DeviceExtInfo deviceExtInfo = deviceInfo.getDeviceExtInfo();
            jExt.put("chp", deviceExtInfo.getDeviceChp());
            jExt.put("mod", deviceExtInfo.getDeviceMod());
            jExt.put("os", deviceExtInfo.getDeviceOS());
            jExt.put("tc_ver", deviceExtInfo.getDeviceTcVer());
            jExt.put("ver", deviceExtInfo.getDeviceVer());
            jExt.put("emcId", deviceExtInfo.getEmcId());
            jExt.put("openId", deviceExtInfo.getOpenId());
            jExt.put("activeId", deviceExtInfo.getActiveId());
            jExt.put("txId", deviceExtInfo.getTxId());
            jExt.put("brand", deviceExtInfo.getBrand());
            jExt.put("androidVer", deviceExtInfo.getAndroidVer());
            jExt.put("pushVer", deviceExtInfo.getPushVer());
            jRoot.put("ext", jExt);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jRoot;
    }

    public static JSONObject getRegIdQJsonData(String clientId, AppInfo appInfo) {
        JSONObject jRoot = new JSONObject();
        String appId = appInfo.getAppId();
        String appkey = appInfo.getAppKey();
        String version = appInfo.getVersion();
        String appName = appInfo.getAppName();
        String packageName = appInfo.getPackageName();
        String now = String.valueOf(System.currentTimeMillis() / 1000);
        String crypt = appId + clientId + appkey + now;
        String token = MD5Helper.getMD5Digest16(crypt);
        try {
            jRoot.put("servid", appId);
            jRoot.put("udid", clientId);
            jRoot.put("time", now);
            jRoot.put("token", token);
            jRoot.put("version", version);
            jRoot.put("appName", appName);
            jRoot.put("packageName", packageName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jRoot;
    }

    public static JSONObject getErrJsonData() {
        JSONObject jRoot = new JSONObject();
        try {
            jRoot.put("reason", "connect service fail!");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jRoot;
    }
}
